{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "raw_mimetype": "text/restructuredtext"
   },
   "source": [
    ".. _nb_custom_output:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Display"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "An algorithm prints by default some information each generation. However, this might be desired to be modified.\n",
    "To do that a `Display` object can be passed to the `minimize` function (or also to the `Algorithm` constructor) to override the default behavior.\n",
    "\n",
    "For instance:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "code": "algorithms/usage_custom_output.py"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=============================================\n",
      "n_gen |  n_eval |   metric_a   |   metric_b  \n",
      "=============================================\n",
      "    1 |     100 |  0.500122773 |  2.958637098\n",
      "    2 |     200 |  0.457137310 |  2.716284231\n",
      "    3 |     300 |  0.424878151 |  2.540654646\n",
      "    4 |     400 |  0.394032982 |  2.404824717\n",
      "    5 |     500 |  0.370407264 |  2.295560288\n",
      "    6 |     600 |  0.352432747 |  2.213395359\n",
      "    7 |     700 |  0.328691523 |  2.099318577\n",
      "    8 |     800 |  0.309089487 |  1.997100714\n",
      "    9 |     900 |  0.291940743 |  1.918699384\n",
      "   10 |    1000 |  0.272572521 |  1.832162449\n"
     ]
    }
   ],
   "source": [
    "from pymoo.algorithms.nsga2 import NSGA2\n",
    "from pymoo.factory import get_problem\n",
    "from pymoo.optimize import minimize\n",
    "from pymoo.util.display import Display\n",
    "import numpy as np\n",
    "\n",
    "\n",
    "class MyDisplay(Display):\n",
    "\n",
    "    def _do(self, problem, evaluator, algorithm):\n",
    "        super()._do(problem, evaluator, algorithm)\n",
    "        self.output.append(\"metric_a\", np.mean(algorithm.pop.get(\"X\")))\n",
    "        self.output.append(\"metric_b\", np.mean(algorithm.pop.get(\"F\")))\n",
    "\n",
    "\n",
    "problem = get_problem(\"zdt2\")\n",
    "\n",
    "algorithm = NSGA2(pop_size=100)\n",
    "\n",
    "res = minimize(problem,\n",
    "               algorithm,\n",
    "               ('n_gen', 10),\n",
    "               seed=1,\n",
    "               display=MyDisplay(),\n",
    "               verbose=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To just add some printouts of an algorithm you can inherit your own custom display `MyDisplay` from the algorithm's default `Display` object.\n",
    "Whereas some algorithms have already a custom implementation for printing purposes, others just use the default single or multi-objective behavior.\n",
    "For instance, to keep the default output of NSGA2 and simply add the metrics shown above, `MyDisplay` has to inherit from `MultiObjectiveDisplay` and the calling the `super()._do(problem, evaluator, algorithm)` will add the default output."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
